import qqmap from '../../../utils/qqMap'
Page({
  data: {
    address: '',
    village: []
  },
  onLoad() {
    this.getLocation()
  },
  //获取当前位置
  async getLocation() {
    const { latitude, longitude } = await wx.getLocation()
    console.log(latitude, longitude)
    this.getAddress(latitude, longitude)
  },
  //获取重新选择定位
  async chooseLocation() {
    const { latitude, longitude } = await wx.chooseLocation()
    console.log(latitude, longitude)
    this.getAddress(latitude, longitude)
  },
  //获取重新选择定位地址
  getAddress(latitude, longitude) {
    qqmap.reverseGeocoder({
      location: `${latitude},${longitude}`,
      success: (res) => {
        console.log(res);
        const { address } = res.result
        this.setData({
          address
        })
      }
    }),
      qqmap.search({
        keyword: '住宅小区',  //搜索关键词
        location: `${latitude},${longitude}`,  //设置周边搜索中心点
        page_size: 5,
        success: (res) => {
          const village = res.data.map(item => {
            return {
              id: item.id,
              title: item.title,
              distance: item._distance
            }
          })
          this.setData({
            village
          })
        },
        fail: () => {
          this.setData({
            "mockData.point": "建材小区"
          })
        }
      })
  }
})